// run: $exec < input
// opt: 0
// flag: -g
#include <iostream>
#include <algorithm>
#include <vector>

int const maxn = 230100;
int n;

struct node
{
	node() { child[0] = child[1] = parent = NULL; }

	void set_child(node * c, bool is_right)
	{
		child[is_right] = c;
		c->parent = this;
		c->is_right = is_right;
	}

	void add(int d) { delta += d; min += d; key += d; }

	int key, size, delta, min;
	bool rev; // interval reverse flag
	bool is_right;
	node * child[2], * parent;
} * null, memory[maxn], * alloc = memory;

std::vector<std::vector<int>> evil;
std::vector<node *> evil_root;
std::vector<node *> evil_last;
node * all_root;
node * all_last;

namespace splay_tree
{
	void update(node * t)
	{
		t->size = t->child[0]->size + t->child[1]->size + 1;
		t->min = std::min(t->key, std::min(t->child[0]->min, t->child[1]->min));
	}

	node * new_node(int key)
	{
		alloc->child[0] = alloc->child[1] = null;
		alloc->rev = false;
		alloc->size = 1;
		alloc->key = key;
		alloc->delta = 0;
		alloc->min = key;
		return alloc++;
	}

	void reverse(node * t)
	{
		if (t == null) return;
		t->rev ^= 1;
		std::swap(t->child[0], t->child[1]);
		t->child[0]->is_right = false;
		t->child[1]->is_right = true;
	}

	void push_down(node * t)
	{
		if (t->delta) {
			for (int i = 0; i < 2; i++)
				if (t->child[i] != null)
					t->child[i]->add(t->delta);
		}
		if (t->rev) {
			reverse(t->child[0]);
			reverse(t->child[1]);
		}
		t->rev = false;
	}

	void rot(node * t, node * root)
	{
		node * p = t->parent;
		push_down(p);
		push_down(t);
		bool ir = t->is_right;
		p->set_child(t->child[!ir], ir);
		p->parent->set_child(t, p->is_right);
		t->set_child(p, !ir);
		update(p);
		update(t);
		if (p == root) root = t;
	}

	void splay(node * t, node * f, node * root)
	{
		// for (push_down(t); t->parent != f; ) {
		for (; t->parent != f; ) { // no need push down. ac.
			if (t->parent->parent == f)
				rot(t, root);
			else
				t->is_right == t->parent->is_right ? (rot(t->parent, root), rot(t, root)) : (rot(t, root), rot(t, root));
		}
	}

	node * sel(int k, node * root)
	{
		for (node * t = root; ; ){
			push_down(t);
			int tmp = t->child[0]->size;
			if (tmp == k) return t;
			t = t->child[k > tmp];
			if (k > tmp) k -= tmp + 1;
		}
	}

	void init()
	{
		null = new node; null->size = 0;
		evil_root.resize(n);
		evil_last.resize(n);
		for (int i = 0; i < n; i++) {
			evil_root[i] = new_node(0); evil_root[i]->parent = null;
			node * p, * q = evil_root[i];
			for (int j = 0; j < (int)evil[i].size(); j++) {
				p = new_node(evil[i][j] - j);
				q->set_child(p, true);
				update(q);
				q = p;
			}
			evil_last[i] = new_node(0);
			q->set_child(evil_last[i], true);
			splay(evil_last[i], null, evil_root[i]);
		}
		all_root = new_node(0); all_root->parent = null;
		node * p, * q = all_root;
		for (int i = 0; i < n; i++) {
		}
	}

	void reverse(int l, int r, node * root)
	{
		node * x, * y;
		x = sel(l - 1, root);
		splay(x, null, root);
		y = sel(r + 1, root);
		splay(y, root, root);
		reverse(y->child[0]);
	}

	void print(node * t)
	{
		if (t == null || !t) return;
		push_down(t);
		print(t->child[0]);
		if (t->key) std::printf("%d ", t->key);
		print(t->child[1]);
	}
}

struct team { int def, tot; };
bool operator<(team const& a, team const& b)
{
	return a.def < b.def || (a.def == b.def && a.tot > b.tot);
}

int calc()
{
	std::vector<team> v;
	for (int i = 0; i < n; i++) {
		team tmp; tmp.def = 0; tmp.tot = evil[i].size();
		for (int j = 0; j < (int)evil[i].size(); j++)
			tmp.def = std::max(tmp.def, evil[i][j] - j);
		v.push_back(tmp);
	}
	std::sort(v.begin(), v.end());
//	for (auto i : v) std::cout << i.def << ' ' << i.tot << '\n';
	int ret = 0, now = 0;
	for (int i = 0; i < n; i++) {
		if (v[i].def > now) {
			ret += v[i].def - now;
			now += v[i].def - now;
		}
		now += v[i].tot;
	}
	return ret;
}

void rotate(int x, int a, int y, int b)
{
}

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin >> n;
	evil.resize(n);
	for (int i = 0; i < n; i++) {
		int m;
		std::cin >> m;
		for (int j = 0, x; j < m; j++) {
			std::cin >> x;
			evil[i].push_back(x);
		}
	}

	splay_tree::init();

	int q; std::cin >> q;
	for (int i = 0; i < q; i++) {
		int x, a, y, b;
		std::cin >> x >> a >> y >> b;
		x--; y--;
		rotate(x, a, y, b);
		std::cout << calc() << '\n';
	}
}

